package com.lanou.myapp.news.movienews;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.VolleySingleton;
import com.lanou.myapp.news.movienews.contentmoviesnews.ContentMoviesNewsActivity;
import com.lanou.myapp.progressdialog.Dialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/10.
 */
public class MovieNewsFragment extends Fragment {
    private Context context;
    private ListView listView;
    private MovieNewsAdapter adapter;
    private String url = "http://dailyapi.ibaozou.com/api/v1/section/18";
    private List<Articles> data;
    private Dialog dialog;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = (ListView) getActivity().findViewById(R.id.listView_movie_news);

        dialog = new Dialog(context,"");
        dialog.setTitle("正在加载，请稍后...");
        dialog.show();

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                data = new ArrayList<>();
                Gson gson = new Gson();
                ArticlesBeans beans = gson.fromJson(response, ArticlesBeans.class);
                data = beans.getArticles();
                adapter = new MovieNewsAdapter(context);
                adapter.addData(data);
                listView.setAdapter(adapter);
                dialog.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        VolleySingleton.getInstance()._addRequest(request);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ContentMoviesNewsActivity.class);
                intent.putExtra("key", data.get(position).getShare_url());
                getActivity().startActivity(intent);
            }
        });

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_movie, null);
        return view;
    }
}
